﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace StockTrading.Migrations
{
    public partial class ini : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "stock",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    name = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_stock", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    name = table.Column<string>(nullable: false),
                    login_name = table.Column<string>(nullable: false),
                    password = table.Column<string>(maxLength: 16, nullable: false),
                    type = table.Column<bool>(nullable: false),
                    cny_free = table.Column<decimal>(nullable: false),
                    cny_freezed = table.Column<decimal>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "order",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    create_date = table.Column<DateTime>(nullable: false),
                    user_id = table.Column<int>(nullable: false),
                    stock_id = table.Column<int>(nullable: false),
                    type = table.Column<byte>(type: "tinyint", nullable: false),
                    price = table.Column<decimal>(nullable: false),
                    undealed = table.Column<decimal>(nullable: false),
                    dealed = table.Column<decimal>(nullable: false),
                    canceled = table.Column<decimal>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_order", x => x.id);
                    table.ForeignKey(
                        name: "FK_order_stock_stock_id",
                        column: x => x.stock_id,
                        principalTable: "stock",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_order_users_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "user_position",
                columns: table => new
                {
                    user_id = table.Column<int>(nullable: false),
                    stockid = table.Column<int>(nullable: false),
                    num_free = table.Column<decimal>(nullable: false),
                    num_freezed = table.Column<decimal>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_position", x => new { x.stockid, x.user_id });
                    table.ForeignKey(
                        name: "FK_user_position_stock_stockid",
                        column: x => x.stockid,
                        principalTable: "stock",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_user_position_users_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "transaction",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    buy_order_id = table.Column<int>(nullable: false),
                    sell_order_id = table.Column<int>(nullable: false),
                    dealed = table.Column<decimal>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_transaction", x => x.id);
                    table.ForeignKey(
                        name: "FK_transaction_order_buy_order_id",
                        column: x => x.buy_order_id,
                        principalTable: "order",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_transaction_order_sell_order_id",
                        column: x => x.sell_order_id,
                        principalTable: "order",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_order_stock_id",
                table: "order",
                column: "stock_id");

            migrationBuilder.CreateIndex(
                name: "IX_order_user_id",
                table: "order",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_transaction_buy_order_id",
                table: "transaction",
                column: "buy_order_id");

            migrationBuilder.CreateIndex(
                name: "IX_transaction_sell_order_id",
                table: "transaction",
                column: "sell_order_id");

            migrationBuilder.CreateIndex(
                name: "IX_user_position_user_id",
                table: "user_position",
                column: "user_id");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "transaction");

            migrationBuilder.DropTable(
                name: "user_position");

            migrationBuilder.DropTable(
                name: "order");

            migrationBuilder.DropTable(
                name: "stock");

            migrationBuilder.DropTable(
                name: "users");
        }
    }
}
